/* OPTIONS OBS=5000  NOREPLACE ; */  

%include "ASMimplibs.sas";


/** 
   Created 05/06/2015.

   This program reads in all tabulated non-AR plants from 2002 and 2007 CMFs.

   Then save the gooddata files (with imputes made missing) to
   the allcmf library.  I will use these files to create new CART imputations
   for all manufacturing industries.  

   This program only makes imputed data missing if the imputes are industry average ratio imputes 
   (edit/impute flag = "RV or " V") or univariate regression imputes 
   (edit/impute flag = "RB" or " B" and does not use prior-year ASM data). 

   INPUT FILES:    
    allcmf.cmf2002_nonar.sas7bdat

    allcmf.cmf07asm06.sas7bdat

   OUTPUT FILES:

    allcmf.gooddata_all_inds02.sas7bdat
    allcmf.gooddata_all_inds07.sas7bdat

**/


data allinds02 (KEEP= survu_id firmid tab tab_f tae tae_f  ee ee_f 
           cf cf_f cm cm_f ph ph_f  ww ww_f sw te te_f tvs tvs_f tib tib_f tie tie_f NAICS_NEW_6 /*
          */ TE_Beta_model7 TVS_Beta_model7 TIB_Beta_model7 TIE_Beta_model7 CM_Beta_model7 WW_Beta_model7 /*
          */ PH_Beta_model7  );
 set allcmf.cmf2002_nonar; 
run;


data allinds07 (KEEP=survu_id firmid tab tab_f tae tae_f ee ee_f 
           cf cf_f cm cm_f ph ph_f  ww ww_f sw te te_f tvs tvs_f tib tib_f tie tie_f NAICS_NEW_6 /*
          */ TE_Beta_model7 TVS_Beta_model7 TIB_Beta_model7 TIE_Beta_model7 CM_Beta_model7 WW_Beta_model7 /*
          */ PH_Beta_model7);
 set allcmf.cmf07asm06 ;
run;


%MACRO getgooddata(year=);

data allcmf.gooddata_all_inds&year (keep =survu_id firmid NAICS_NEW_6 tvs tib tie cm ee cf sw te ph ww tae year);
 set allinds&year;

            if TE_F in (' B','RB') and TE_Beta_model7=0 
            then te = .;

            if TVS_F in (' B','RB') and TVS_Beta_model7=0 
            then tvs = .;
            else if TVS_F in (' V','RV')
            then tvs = .;

            if tib_F in (' B','RB') and tib_Beta_model7=0
            then tib = .;

            if tie_F in (' B','RB') and tie_Beta_model7=0
            then tie = .;

            if cm_F in (' B','RB') and CM_Beta_model7=0
            then cm = .;

            if ee_f in (' V','RV') 
            then ee = .;

            if cf_f in (' V','RV') 
            then cf = .;

            if PH_F in (' B','RB') and PH_Beta_model7=0
            THEN ph = .;

            if WW_F in (' B','RB') and WW_Beta_model7=0
            THEN WW = .;

            year = "20&year";

        run;

%MEND ;


%getgooddata(year=02);
%getgooddata(year=07);


